import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class FourthWindow extends JFrame {
    private JTextField dateField, amountField, reasonField;
    private JButton addButton, deleteButton, updateButton, searchButton;
    private JTable detailsTable;
    private DefaultTableModel tableModel;

    public FourthWindow() {
        setTitle("财务管理");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(500, 300);
        setLocationRelativeTo(null);

        initializeComponents();
        addComponentsToFrame();
    }

    private void initializeComponents() {
        dateField = new JTextField(10);
        amountField = new JTextField(10);
        reasonField = new JTextField(20);

        addButton = new JButton("添加");
        deleteButton = new JButton("删除");
        updateButton = new JButton("更新");
        searchButton = new JButton("搜索");

        // 创建表格模型和表格对象
        String[] columnNames = {"日期", "金额", "原因"};
        tableModel = new DefaultTableModel(columnNames, 0);
        detailsTable = new JTable(tableModel);
    }

    private void addComponentsToFrame() {
        JPanel inputPanel = new JPanel(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.anchor = GridBagConstraints.WEST;
        gbc.insets = new Insets(5, 5, 5, 5);
        inputPanel.add(new JLabel("日期："), gbc);
        gbc.gridx++;
        inputPanel.add(dateField, gbc);

        gbc.gridx = 0;
        gbc.gridy++;
        inputPanel.add(new JLabel("金额："), gbc);
        gbc.gridx++;
        inputPanel.add(amountField, gbc);

        gbc.gridx = 0;
        gbc.gridy++;
        inputPanel.add(new JLabel("原因："), gbc);
        gbc.gridx++;
        inputPanel.add(reasonField, gbc);

        gbc.gridx = 0;
        gbc.gridy++;
        gbc.gridwidth = 2;
        inputPanel.add(addButton, gbc);

        gbc.gridy++;
        inputPanel.add(deleteButton, gbc);

        gbc.gridy++;
        inputPanel.add(updateButton, gbc);

        gbc.gridy++;
        inputPanel.add(searchButton, gbc);

        add(inputPanel, BorderLayout.WEST);
        add(new JScrollPane(detailsTable), BorderLayout.CENTER);

        // 添加按钮点击事件监听器
        addButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String date = dateField.getText();
                String amount = amountField.getText();
                String reason = reasonField.getText();

                if (!date.isEmpty() && !amount.isEmpty() && !reason.isEmpty()) {
                    String[] rowData = {date, amount, reason};
                    tableModel.addRow(rowData);

                    // 清空输入框
                    dateField.setText("");
                    amountField.setText("");
                    reasonField.setText("");
                } else {
                    JOptionPane.showMessageDialog(FourthWindow.this,
                            "请输入完整的信息", "错误", JOptionPane.ERROR_MESSAGE);
                }
            }
        });

        deleteButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                int selectedRow = detailsTable.getSelectedRow();
                if (selectedRow != -1) {
                    tableModel.removeRow(selectedRow);
                } else {
                    JOptionPane.showMessageDialog(FourthWindow.this,
                            "请选择要删除的行", "错误", JOptionPane.ERROR_MESSAGE);
                }
            }
        });

        updateButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                int selectedRow = detailsTable.getSelectedRow();
                if (selectedRow != -1) {
                    String date = dateField.getText();
                    String amount = amountField.getText();
                    String reason = reasonField.getText();

                    if (!date.isEmpty() && !amount.isEmpty() && !reason.isEmpty()) {
                        tableModel.setValueAt(date, selectedRow, 0);
                        tableModel.setValueAt(amount, selectedRow, 1);
                        tableModel.setValueAt(reason, selectedRow, 2);

                        // 清空输入框
                        dateField.setText("");
                        amountField.setText("");
                        reasonField.setText("");
                    } else {
                        JOptionPane.showMessageDialog(FourthWindow.this,
                                "请输入完整的信息", "错误", JOptionPane.ERROR_MESSAGE);
                    }
                } else {
                    JOptionPane.showMessageDialog(FourthWindow.this,
                            "请选择要更新的行", "错误", JOptionPane.ERROR_MESSAGE);
                }
            }
        });

        searchButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String keyword = JOptionPane.showInputDialog(FourthWindow.this,
                        "请输入要搜索的关键词");
                if (keyword != null && !keyword.isEmpty()) {
                    for (int i = 0; i < tableModel.getRowCount(); i++) {
                        String rowData = tableModel.getValueAt(i, 0).toString()
                                + tableModel.getValueAt(i, 1).toString()
                                + tableModel.getValueAt(i, 2).toString();

                        if (rowData.contains(keyword)) {
                            detailsTable.setRowSelectionInterval(i, i);
                            detailsTable.scrollRectToVisible(detailsTable.getCellRect(i, 0, true));
                            break;
                        }
                    }
                }
            }
        });
    }

    /*public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new FourthWindow().setVisible(true);
            }
        });
    }*/
}
